package com.ruoyi.college.controller;

import com.ruoyi.college.domain.College;
import com.ruoyi.college.domain.Major;
import com.ruoyi.college.domain.ResearchGroup;
import com.ruoyi.college.service.IMajorService;
import com.ruoyi.college.service.IResearchGroupService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.SysUserRole;
import com.ruoyi.system.mapper.SysUserRoleMapper;
import com.ruoyi.system.service.ISysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 教研室信息Controller
 * 
 * @author ruoyi
 * @date 2025-08-22
 */
@Controller
@RequestMapping("/college/group")
public class ResearchGroupController extends BaseController
{
    private String prefix = "college/group";

    @Autowired
    private IResearchGroupService researchGroupService;

    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;

    @Autowired
    private IMajorService majorService;

    @Autowired
    private ISysUserService userService;

    @RequiresPermissions("college:group:view")
    @GetMapping()
    public String group(ModelMap mmap)
    {
        //返回下拉
        //专业信息
        mmap.put("majors", majorService.selectMajorList(new Major()));
        //教研室主任
        Long roleId = 101L;
        List<SysUserRole> sysUserRoles = sysUserRoleMapper.selectUserRoleByRoleId(roleId);
        List<Long> userIds = sysUserRoles.stream().map(SysUserRole::getUserId).collect(Collectors.toList());
        List<SysUser> directors = userService.selectUserListByIds(userIds);
        mmap.put("directors", directors);
        return prefix + "/group";
    }

    /**
     * 查询教研室信息列表
     */
    @RequiresPermissions("college:group:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ResearchGroup researchGroup)
    {
        startPage();
        List<ResearchGroup> list = researchGroupService.selectResearchGroupList(researchGroup);
        return getDataTable(list);
    }

    /**
     * 导出教研室信息列表
     */
    @RequiresPermissions("college:group:export")
    @Log(title = "教研室信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ResearchGroup researchGroup)
    {
        List<ResearchGroup> list = researchGroupService.selectResearchGroupList(researchGroup);
        ExcelUtil<ResearchGroup> util = new ExcelUtil<ResearchGroup>(ResearchGroup.class);
        return util.exportExcel(list, "教研室信息数据");
    }



    /**
     * 新增教研室信息
     */
    @RequiresPermissions("college:group:add")
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        //专业信息
        mmap.put("majors", majorService.selectMajorList(new Major()));
        //教研室主任
        Long roleId = 101L;
        List<SysUserRole> sysUserRoles = sysUserRoleMapper.selectUserRoleByRoleId(roleId);
        List<Long> userIds = sysUserRoles.stream().map(SysUserRole::getUserId).collect(Collectors.toList());
        List<SysUser> directors = userService.selectUserListByIds(userIds);
        mmap.put("directors", directors);
        return prefix + "/add";
    }

    /**
     * 新增保存教研室信息
     */
    @RequiresPermissions("college:group:add")
    @Log(title = "教研室信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ResearchGroup researchGroup)
    {
        return toAjax(researchGroupService.insertResearchGroup(researchGroup));
    }

    /**
     * 修改教研室信息
     */
    @RequiresPermissions("college:group:edit")
    @GetMapping("/edit/{groupId}")
    public String edit(@PathVariable("groupId") Long groupId, ModelMap mmap)
    {
        //专业信息
        mmap.put("majors", majorService.selectMajorList(new Major()));
        //教研室主任
        Long roleId = 101L;
        List<SysUserRole> sysUserRoles = sysUserRoleMapper.selectUserRoleByRoleId(roleId);
        List<Long> userIds = sysUserRoles.stream().map(SysUserRole::getUserId).collect(Collectors.toList());
        List<SysUser> directors = userService.selectUserListByIds(userIds);
        mmap.put("directors", directors);
        ResearchGroup researchGroup = researchGroupService.selectResearchGroupByGroupId(groupId);
        mmap.put("researchGroup", researchGroup);
        return prefix + "/edit";
    }

    /**
     * 修改保存教研室信息
     */
    @RequiresPermissions("college:group:edit")
    @Log(title = "教研室信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ResearchGroup researchGroup)
    {
        return toAjax(researchGroupService.updateResearchGroup(researchGroup));
    }

    /**
     * 删除教研室信息
     */
    @RequiresPermissions("college:group:remove")
    @Log(title = "教研室信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(researchGroupService.deleteResearchGroupByGroupIds(ids));
    }
}
